import {ref} from "vue";
import type {FileItem} from "@/views/documentManager/components/ts/FileBrowser";

/** ----------------------------
 *  拖拽相关状态
 */
export const draggedItem = ref<FileItem | null>(null);
export const dragOverItem = ref<string | null>(null);
export const dragOverParentFolder = ref<boolean>(false);

// 拖拽开始
export const handleDragStart = (item: FileItem, event: DragEvent) => {
    draggedItem.value = item;
    // 设置拖拽数据
    event.dataTransfer?.setData('text/plain', JSON.stringify(item));
};

// 拖拽结束
export const handleDragEnd = () => {
    draggedItem.value = null;
    dragOverItem.value = null;
    dragOverParentFolder.value = false;
};

// 上一级目录拖拽经过
export const handleParentFolderDragOver = (event: DragEvent) => {
    event.preventDefault();
    event.stopPropagation();
    if (draggedItem.value) {
        dragOverParentFolder.value = true;
    }
};


// 拖拽经过
export const handleDragOver = (item: FileItem) => {
    if (item.folderFlag && draggedItem && draggedItem.value?.path !== item.path) {
        dragOverItem.value = item.path;
    }
};

// 上一级目录拖拽离开
export const handleParentFolderDragLeave = () => {
    dragOverParentFolder.value = false;
};

// 拖拽离开
export const handleDragLeave = (item: FileItem) => {
    if (dragOverItem.value === item.path) {
        dragOverItem.value = null;
    }
};